package com.xy.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.model.BJi;
import com.xy.model.Category;
import com.xy.model.Level;
import com.xy.model.Major;
import com.xy.model.PackageBook;
import com.xy.model.Station;
import com.xy.model.TextBook;
import com.xy.service.impl.CategoryServiceImpl;
import com.xy.service.impl.ClassServiceImpl;
import com.xy.service.impl.LevelServiceImpl;
import com.xy.service.impl.MajorServiceImpl;
import com.xy.service.impl.PackageBookServiceImpl;
import com.xy.service.impl.StationServiceImpl;
import com.xy.service.impl.TextBookServiceImpl;

/**
 * Servlet implementation class DeliverServlet
 */
@WebServlet("/deliverServlet")
public class DeliverServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeliverServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Object> clist = new CategoryServiceImpl().list(Category.class);
		List<Object> mlist = new MajorServiceImpl().list(Major.class);
		List<Object> llist = new LevelServiceImpl().list(Level.class);
		List<TextBook> tbs = new ArrayList();
		request.setAttribute("clist", clist);
		request.setAttribute("mlist", mlist);
		request.setAttribute("llist", llist);

		if ("gotoedit".equals(request.getParameter("status"))) {
			request.getSession().setAttribute("tbs", tbs);
			request.getRequestDispatcher("/pages/storage/storage001/student/edit_package.jsp").forward(request,
					response);
		} else if ("add".equals(request.getParameter("status"))) {
			// 得到教材id
			// 传教材到页面显示
			if (request.getSession().getAttribute("tbs") != null) {
				tbs = (List<TextBook>) request.getSession().getAttribute("tbs");
			}

			Integer textBookId = Integer.valueOf(request.getParameter("radio"));
			tbs.add(new TextBookServiceImpl().get(textBookId));
			request.getSession().setAttribute("tbs", tbs);
			request.getRequestDispatcher("/pages/storage/storage001/student/edit_package.jsp").forward(request,
					response);

		} else if ("student".equals(request.getParameter("status"))) {
			request.getRequestDispatcher("/pages/storage/storage001/student/query_package.jsp").forward(request,
					response);
		} else if ("teaadd".equals(request.getParameter("status"))) {
			// 根据教材id获取架位教材信息
			Integer textBookId = Integer.valueOf(request.getParameter("radio"));

			request.setAttribute("id", textBookId);

			request.getRequestDispatcher("/pages/storage/storage001/teacher/teacher.jsp").forward(request, response);
		} else if ("staadd".equals(request.getParameter("status"))) {
			Integer textBookId = Integer.valueOf(request.getParameter("radio"));
			request.setAttribute("stations", new StationServiceImpl().list(Station.class));
			request.setAttribute("id", textBookId);

			request.getRequestDispatcher("/pages/storage/storage001/station/station.jsp").forward(request, response);
		} else if ("othadd".equals(request.getParameter("status"))) {
			Integer textBookId = Integer.valueOf(request.getParameter("radio"));
			request.setAttribute("id", textBookId);

			request.getRequestDispatcher("/pages/storage/storage001/other/other.jsp").forward(request, response);
		}

	}

}
